/* Start Header **************************************************************/
/*!
\file Vector2D.cpp
\author David Seah Seng Chong, sengchong.seah, 250004910
\par sengchong.seah\@digipen.edu)
\date June 18, 2011)
\brief
Copyright (C) 2011 DigiPen Institute of Technology.
Reproduction or disclosure of this file or its contents
without the prior written consent of DigiPen Institute of
Technology is prohibited.
*/
/* End Header ****************************************************************/

#include "PreCompiledHeader.h"
#include "Vector2D.h"
#include <math.h>


	

	/**************************************************************************/
	/*!
		Set the x and y components of the vector
	 */
	/**************************************************************************/
	Vector2D::Vector2D(float _x, float _y)
	{
		x = _x;
		y = _y;
	}

	/**************************************************************************/
	/*!
		Adding two vectors and the result should be stored 
		into the object itself
	 */
	/**************************************************************************/
	Vector2D& Vector2D::operator += (const Vector2D &rhs)
	{
		x += rhs.x;
		y += rhs.y;
		return *this;
	}

	/**************************************************************************/
	/*!
		Subtracting two vectors and the result should be stored 
		into the object itself
	 */
	/**************************************************************************/
	Vector2D& Vector2D::operator -= (const Vector2D &rhs)
	{
		 x -= rhs.x;
		 y -= rhs.y;
		 return *this;
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into the object itself
	 */
	/**************************************************************************/
	Vector2D& Vector2D::operator *= (float rhs)
	{
		x *= rhs;
		y *= rhs;
		return *this;	
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into the object itself
	 */
	/**************************************************************************/
	Vector2D& Vector2D::operator /= (float rhs)
	{
		x /= rhs;
		y /= rhs;
		return *this;
	}

	/**************************************************************************/
	/*!
		Negating the vector and the result is stored into a new vector
	 */
	/**************************************************************************/
	Vector2D Vector2D::operator-() const
	{
		 return Vector2D(-x, -y);
	}


	// Non-member functions
	//-------------------------------------------------------------------------

	/**************************************************************************/
	/*!
		Adding two vectors and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator + (const Vector2D &lhs, const Vector2D &rhs)
	{
		return Vector2D(lhs.x + rhs.x, lhs.y + rhs.y);
	}

	/**************************************************************************/
	/*!
		Subtracting two vectors and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator - (const Vector2D &lhs, const Vector2D &rhs)
	{
		 return Vector2D(lhs.x - rhs.x, lhs.y - rhs.y);
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator * (const Vector2D &lhs, float rhs)
	{
		return Vector2D(lhs.x * rhs, lhs.y * rhs);
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator * (float lhs, const Vector2D &rhs)
	{
		return Vector2D(lhs * rhs.x, lhs * rhs.y);
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator / (const Vector2D &lhs, float rhs)
	{
		return Vector2D(lhs.x / rhs,  lhs.y / rhs);
	}

	/**************************************************************************/
	/*!
		Scaling a vector and the result should be stored 
		into a new vector
	 */
	/**************************************************************************/
	Vector2D operator / (float lhs, const Vector2D &rhs)
	{
		return Vector2D(lhs / rhs.x, lhs / rhs.y);
	}

	/**************************************************************************/
	/*!
		In this function, pResult will be the unit vector of pVec0
	 */
	/**************************************************************************/
	void Vector2DNormalize(Vector2D &pResult, const Vector2D &pVec0)
	{
		pResult = pVec0 / sqrtf(pVec0.x * pVec0.x + pVec0.y * pVec0.y);
	}

	/**************************************************************************/
	/*!
		This function returns the length of the vector pVec0 
	 */
	/**************************************************************************/
	float Vector2DLength(const Vector2D &pVec0)
	{
		return sqrt(pVec0.x * pVec0.x + pVec0.y * pVec0.y);
	}

	/**************************************************************************/
	/*!
		This function returns the square of pVec0's length. 
		Avoid the square root 
	 */
	/**************************************************************************/
	float Vector2DSquareLength(const Vector2D &pVec0)
	{
		 return pVec0.x * pVec0.x + pVec0.y * pVec0.y;
	}

	/**************************************************************************/
	/*!
		In this function, pVec0 and pVec1 are considered as 2D points.
		The distance between these 2 2D points is returned
	 */
	/**************************************************************************/
	float Vector2DDistance(const Vector2D &pVec0, const Vector2D &pVec1)
	{
		return sqrt
		(
			(pVec1.y - pVec0.y) * (pVec1.y - pVec0.y) +
			(pVec1.x - pVec0.x) * (pVec1.x - pVec0.x)
		);
	}

	/**************************************************************************/
	/*!
		In this function, pVec0 and pVec1 are considered as 2D points.
		The squared distance between these 2 2D points is returned. 
		Avoid the square root
	 */
	/**************************************************************************/
	float Vector2DSquareDistance(const Vector2D &pVec0, const Vector2D &pVec1)
	{
		 return 
		(
			(pVec1.y - pVec0.y) * (pVec1.y - pVec0.y) +
			(pVec1.x - pVec0.x) * (pVec1.x - pVec0.x)
		);
	}

	/**************************************************************************/
	/*!
		This function returns the dot product between pVec0 and pVec1
	 */
	/**************************************************************************/
	float Vector2DDotProduct(const Vector2D &pVec0, const Vector2D &pVec1)
	{
		return pVec0.x * pVec1.x + pVec0.y * pVec1.y;
	}

	/**************************************************************************/
	/*!
		This functions return the cross product magnitude 
		between pVec0 and pVec1
	 */
	/**************************************************************************/
	float Vector2DCrossProductMag(const Vector2D &pVec0, const Vector2D &pVec1)
	{
		 return pVec0.x * pVec1.y - pVec0.y * pVec1.x;
	}
